package com.lastproject.web.bean;

import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.bean.SessionScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ComponentSystemEvent;

import com.lastproject.dto.CandidateDTO;
import com.lastproject.dto.EmployeeDTO;
import com.lastproject.dto.InterviewDTO;
import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.dao.CandidateDAORemote;
import com.lastproject.ejb.dao.CheckPermissionDAORemote;
import com.lastproject.ejb.dao.EmployeeDAORemote;
import com.lastproject.ejb.dao.InterviewDAORemote;

@ManagedBean
@SessionScoped
public class InterviewBean {

	// @ManagedProperty(value = "#{employeeDTO}")
	private EmployeeDTO employee;

	// @ManagedProperty(value = "#{interviewDTO}")
	private InterviewDTO interviewDTO;

	// @ManagedProperty(value = "#{employeeDTO}")
	private EmployeeDTO employeeDTO;

	@ManagedProperty(value = "#{candidateBean}")
	private CandidateBean canBean;

	@ManagedProperty(value = "#{userBean}")
	private UserBean userBean;

	// @ManagedProperty(value = "#{employeeBean}")
	private EmployeeBean employeeBean;

	@EJB
	InterviewDAORemote interviewDAORemote;

	@EJB
	CandidateDAORemote candidateDAORemote;

	@EJB
	EmployeeDAORemote employeeDAORemote;

	private String subject;
	private String sender;
	private String receiver;
	private String content;

	private String errorMessage="";
	private String errorMessageSend="";
	private String errorMessageDelete="";
	private String errorMessageUpdate="";

	private List<InterviewDTO> interviews;
	private List<EmployeeDTO> employees;

	private CandidateDTO candidateDTO;

	private int idInterviewForDeletion;
	private boolean deletion = false;
	private int selectedEmployeeId;
	
	@EJB
	CheckPermissionDAORemote checkPermissionDAOremote;
	
	public void preRenderView(ComponentSystemEvent event) {
        if (!checkPermissionDAOremote.hasCandidateManagementRights(true)){
        	FacesContext fc = FacesContext.getCurrentInstance();
        	ExternalContext ec = fc.getExternalContext();
        	try {
				ec.redirect("restrictedAccess.xhtml");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
    }

	@PostConstruct
	public void init() {

		UserDTO user = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("userDTO");

		this.sender = user.getWorkEmail();

		interviewDTO = new InterviewDTO();

		employees = employeeDAORemote.getAll();

		interviews = interviewDAORemote.getAll();
		deletion = false;
		errorMessage = "";
		errorMessageSend = "";
		errorMessageDelete = "";
		errorMessageUpdate = "";

	}

	public InterviewBean() {

	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public int getIdInterviewForDeletion() {
		return idInterviewForDeletion;
	}

	public void setIdInterviewForDeletion(int idInterviewForDeletion) {
		this.idInterviewForDeletion = idInterviewForDeletion;
	}

	public boolean isDeletion() {
		return deletion;
	}

	public void setDeletion(boolean deletion) {
		this.deletion = deletion;
	}

	public String getErrorMessageUpdate() {
		return errorMessageUpdate;
	}

	public void setErrorMessageUpdate(String errorMessageUpdate) {
		this.errorMessageUpdate = errorMessageUpdate;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public String getErrorMessageSend() {
		return errorMessageSend;
	}

	public void setErrorMessageSend(String errorMessageSend) {
		this.errorMessageSend = errorMessageSend;
	}

	public String getErrorMessageDelete() {
		return errorMessageDelete;
	}

	public void setErrorMessageDelete(String errorMessageDelete) {
		this.errorMessageDelete = errorMessageDelete;
	}

	public List<InterviewDTO> getInterviews() {
		return interviewDAORemote.getAll();
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getSender() {
		// sender = "Alina.Morovan@msg-systems.com";
		return sender;
	}

	public void setSender(String sender) {
		this.sender = sender;
	}

	public String getReceiver() {
		receiver = canBean.getCandidateDTO().getEmail();
		return receiver;
	}

	public void setReceiver(String receiver) {
		this.receiver = canBean.getCandidateDTO().getEmail();
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public InterviewDTO getInterviewDTO() {
		return interviewDTO;
	}

	public void setInterviewDTO(InterviewDTO interviewDTO) {
		this.interviewDTO = interviewDTO;
	}

	public void setInterviews(List<InterviewDTO> interviews) {
		this.interviews = interviews;
	}

	public EmployeeDTO getEmployeeDTO() {
		return employeeDTO;
	}

	public void setEmployeeDTO(EmployeeDTO employeeDTO) {
		this.employeeDTO = employeeDTO;
	}

	public CandidateDTO getCandidateDTO() {
		return candidateDTO;
	}

	public void setCandidateDTO(CandidateDTO candidateDTO) {
		this.candidateDTO = candidateDTO;
	}

	public EmployeeDTO getEmployee() {
		return employee;
	}

	public void setEmployee(EmployeeDTO employee) {
		this.employee = employee;
	}

	public CandidateBean getCanBean() {
		return canBean;
	}

	public void setCanBean(CandidateBean canBean) {
		this.canBean = canBean;
	}

	public UserBean getUserBean() {
		return userBean;
	}

	public void setUserBean(UserBean userBean) {
		this.userBean = userBean;
	}

	public EmployeeBean getEmployeeBean() {
		return employeeBean;
	}

	public void setEmployeeBean(EmployeeBean employeeBean) {
		this.employeeBean = employeeBean;
	}

	public List<EmployeeDTO> getEmployees() {
		return employees;
	}

	public void setEmployees(List<EmployeeDTO> employees) {
		this.employees = employees;
	}

	public List<EmployeeDTO> getListOfEmployee() {
		employees = employeeDAORemote.getAll();

		return employees;

	}

	public int getSelectedEmployeeId() {
		return selectedEmployeeId;
	}

	public void setSelectedEmployeeId(int selectedEmployeeId) {
		this.selectedEmployeeId = selectedEmployeeId;
	}

	public void addInterview() {

		deletion = false;

		interviewDTO.setIdCandidate(canBean.getCandidateDTO().getId());
		interviewDTO.setIdEmployee(selectedEmployeeId);

		if (interviewDTO != null) {
			interviewDAORemote.create(interviewDTO);
			errorMessage = "Interview added succesfuly";
			interviewDTO.setDate(null);
			interviewDTO.setDetails("");
		} else
			errorMessage = "Add failed !";
		
		
		errorMessageSend = "";
		errorMessageDelete = "";
		errorMessageUpdate = "";
	}

	public void sendMail() {

		interviewDAORemote.sendEmailToCandidate(sender, receiver, subject,
				content);
		errorMessageSend = "Message send successfuly !";
		
		errorMessage = "";
		errorMessageDelete = "";
		errorMessageUpdate = "";

	}

	public void delete(int id) {
		deletion = true;
		idInterviewForDeletion = id;
	}

	public void deleteInterview() {

		UserDTO user = (UserDTO) FacesContext.getCurrentInstance()
				.getExternalContext().getSessionMap().get("userDTO");
		interviewDTO = new InterviewDTO();
		interviewDTO = interviewDAORemote.findById(idInterviewForDeletion);

		candidateDTO = candidateDAORemote.findById(interviewDTO
				.getIdCandidate());

		deletion = false;

		interviewDAORemote.remove(interviewDTO);

		System.out.println("sunt aici2");
		// System.out.println("SENDER: "+sender);
		System.out.println("RECEIVER: " + candidateDTO.getEmail());
		System.out.println("DATE la content: " + interviewDTO.getDate());

		// trimit mail doar celor care sunt pe statutul waiting

		System.out
				.println("Statutul candidatului: " + candidateDTO.getStatut());

		if (candidateDTO.getStatut().equals("waiting")) {
			
			String date = dateToString(interviewDTO.getDate());

			// trebuie sa pun mailul userului de pe sesiune
			interviewDAORemote
					.sendEmailToCandidate(
							user.getWorkEmail(),
							candidateDTO.getEmail(),
							"Interviu anulat",
							"Interviul d-voastra din data de "
									+ date
									+ " a fost anulat. Asteptati un nou email cu data reprogramarii. Va multumim!");

			errorMessageDelete = "Interview deleted succesfuly ! Mail sent.";
			init();
			errorMessage = "";
			errorMessageSend = "";
			errorMessageUpdate = "";
		}
	}

	public String checkValues(ActionEvent actionEvent) {

		for (InterviewDTO i : interviews) {

			if (i.getDate().equals(""))
				return errorMessage = "Empty date field!";
			if (i.getDetails().equals(""))
				return errorMessage = "Empty details field!";

			System.out.println("INTERVIU: " + i.getDetails());

			interviewDAORemote.edit(i);

			errorMessageUpdate = "Update succesfuly !";

			init();
			errorMessage = "";
			errorMessageSend = "";
			errorMessageDelete = "";
		
		}

		return null;

	}

	public static String dateToString(Date date) {

		DateFormat dateFormat = new SimpleDateFormat("yyyy.MM.dd");

		String dateString = dateFormat.format(date);

		return dateString;

	}
	
	public String goBack(){
		init();
		errorMessageUpdate = "";
		errorMessage = "";
		errorMessageSend = "";
		errorMessageDelete = "";
		return "listOfCandidates";
	}

}
